package com.ruoyi.bus.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.bus.domain.WitRoute;
import com.ruoyi.bus.domain.WitSite;
import com.ruoyi.bus.service.IWitRouteService;
import com.ruoyi.bus.service.IWitSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.ruoyi.bus.mapper.WitRouteSiteMapper;
import com.ruoyi.bus.domain.WitRouteSite;
import com.ruoyi.bus.service.IWitRouteSiteService;

/**
 * 站点路线Service业务层处理
 * 
 * @author caiyanshan
 * @date 2021-04-14
 */
@Service
public class WitRouteSiteServiceImpl implements IWitRouteSiteService 
{
    @Autowired
    private WitRouteSiteMapper witRouteSiteMapper;

    @Autowired
    private IWitSiteService siteService;
    
    @Autowired
    private IWitRouteService routeService;

    /**
     * 查询站点路线
     * 
     * @param routeId 站点路线ID
     * @return 站点路线
     */
    @Override
    public WitRouteSite selectWitRouteSiteById(Long routeId)
    {
        return witRouteSiteMapper.selectWitRouteSiteById(routeId);
    }
    
    public List<WitRouteSite> selectWitRouteSiteByRouteId(Long routeId){
        return witRouteSiteMapper.selectWitRouteSiteByRouteId(routeId);
    }

    /**
     * 查询站点路线列表
     * 
     * @param witRouteSite 站点路线
     * @return 站点路线
     */
    @Override
    public List<WitRouteSite> selectWitRouteSiteList(WitRouteSite witRouteSite)
    {
        return witRouteSiteMapper.selectWitRouteSiteList(witRouteSite);
    }

    /**
     * 新增站点路线
     * 
     * @param witRouteSite 站点路线
     * @return 结果
     */
    @Override
    @Transient
    public int insertWitRouteSite(WitRouteSite witRouteSite)
    {
        
        /*WitSite witSite = new WitSite();
        witSite.setLatitude(witRouteSite.getLatitude());
        witSite.setLongitude(witRouteSite.getLongitude());
        witSite.setSiteName(witRouteSite.getSiteName());
        siteService.insertWitSite(witSite);*/
        //witRouteSite.setSiteId(witRouteSite.getSiteId());
        //this.updateWitSiteUrl(witRouteSite.getRouteId());
        return witRouteSiteMapper.insertWitRouteSite(witRouteSite);
    }
    
    /**
     * @Description 更新改路线下所有路线播报数据,置为空
     * @Author caiyanshan
     * @Date 2021/4/27 16:08
     * @Param [routeId]
     * @Return void
     */
    private void updateWitSiteUrl(Long routeId){
        WitSite witSite = null;
        WitRoute route = routeService.selectWitRouteByIdNoCache(routeId);
        List<WitRouteSite> witRouteSiteList = route.getWitRouteSiteList();
        for(WitRouteSite routeSite : witRouteSiteList){
            witSite = new WitSite();
            witSite.setSiteId(routeSite.getSiteId());
            witSite.setThisOne("");
            witSite.setNextAm("");
            witSite.setNextPm("");
            siteService.updateWitSite(witSite);//后续可做批量更新
        }
    }
    
    public int updateWitRouteSiteByRouteId(WitRouteSite witRouteSite){
        return witRouteSiteMapper.updateWitRouteSiteByRouteId(witRouteSite);
    }

    /**
     * 修改站点路线
     * 
     * @param witRouteSite 站点路线
     * @return 结果
     */
    @Override
    public int updateWitRouteSite(WitRouteSite witRouteSite)
    {
        //this.updateWitSiteUrl(witRouteSite.getRouteId());
        return witRouteSiteMapper.updateWitRouteSite(witRouteSite);
    }

    /**
     * 批量删除站点路线
     * 
     * @param routeIds 需要删除的站点路线ID
     * @return 结果
     */
    @Override
    public int deleteWitRouteSiteByIds(Long[] routeIds)
    {
        WitRouteSite witRouteSite = witRouteSiteMapper.selectWitRouteSiteById(routeIds[0]);
        this.updateWitSiteUrl(witRouteSite.getRouteId());
        return witRouteSiteMapper.deleteWitRouteSiteByIds(routeIds);
    }

    /**
     * 删除站点路线信息
     * 
     * @param routeId 站点路线ID
     * @return 结果
     */
    @Override
    public int deleteWitRouteSiteById(Long routeId)
    {
        return witRouteSiteMapper.deleteWitRouteSiteById(routeId);
    }
}
